################################################################################
##
## Purpose: This script creates SI Figure 9
##
## Author: James Bisbee (james.h.bisbee@vanderbilt.edu)
##
## Input Files:
##  - ./data/prepped/finalData.RData: Prepped data from 9_DATA_final_build.R
##
## Output Files:
##  - ./output/figures/SI_figure_9.pdf
##
##
## See associated log file for compute environment, package versions, 
##  and date of most recent run.
##
################################################################################
rm(list = ls())
gc()
require(tidyverse)
require(ggridges)
require(ggrepel)
require(marginaleffects)
require(fixest)

set.seed(123)

# Compute details
print(paste0('Compute environment from ',Sys.Date(),' run by Bisbee'))
if(Sys.info()['sysname'] == 'Windows') {
  ram_size = system("wmic MemoryChip get Capacity", intern = TRUE)[-1]
  model_name = system("wmic cpu get name", intern = TRUE)[2] # nocov
  vendor_id = system("wmic cpu get manufacturer", intern = TRUE)[2] # nocov
  
  print(list(ram = stringr::str_squish(ram_size)[1],
             vendor_id = stringr::str_squish(vendor_id),
             model_name = stringr::str_squish(model_name),
             no_of_cores = parallel::detectCores()))
} else if(Sys.info()['sysname'] == 'Linuxs') {
  splitted <- strsplit(system("ps -C rsession -o %cpu,%mem,pid,cmd", intern = TRUE), " ")
  df <- do.call(rbind, lapply(splitted[-1], 
                              function(x) data.frame(
                                cpu = as.numeric(x[2]),
                                mem = as.numeric(x[4]),
                                pid = as.numeric(x[5]),
                                cmd = paste(x[-c(1:5)], collapse = " "))))
  df
} else {
  cat("If not on Linux or Windows, you'll have to figure out your own solution to seeing the compute environment.")
}

sessionInfo()


load('./data/prepped/finalData.RData')

# Heterogeneities
dims <- colnames(utterance_level %>% select(matches('SENT_'),-matches('_lag|error|comb')))


form <- paste0('interruptor ~ yellen*m + poly(log(nchars_lag+1),3) + poly(log(tot_utterances),3) + ',
               paste(paste0('topic_',1:100,'_lag'),collapse = ' + '),
               ' + ',
               paste(paste0('scale(',dims,'_lag)'),collapse = ' + '),
               ' + age + votepct_rel + nominate_dim1 + GOP + gender + ',
               'seniority + anyBill + ',
               'yellen_vote + anyKids',
               ' + interrupted',
               '| docID')

dyadToAnal <- utterance_level %>% 
  mutate(DEM = ifelse(party == 'D',1,0),
         GOP = ifelse(party == 'R',1,0),
         Male = ifelse(gender == 'M',1,0),
         propDaughters = nDaughters / nKids,
         anyDaughters = ifelse(nDaughters > 0,1,0),
         anySons = ifelse(nSons > 0,1,0),
         anyKids = ifelse(nKids > 0,1,0),
         senate = ifelse(chamber == 'Senate',1,0),
         votepct_rel = ifelse(is.infinite(votepct_rel),1,votepct_rel),
         yellen = ifelse(grepl('YELLEN',respondingTo),1,0))

toplot <- NULL
for(ms in c('GOP','Male','age','votepct_rel','seniority','nominate_dim1','anyBill','yellen_vote',
            'senate','anyDaughters','anySons','anyKids')) {
  
  summary(modInt <- feols(as.formula(gsub(paste0(' \\+ ',ms),'',form)),
                         dyadToAnal %>% 
                           mutate(m = get(ms)) %>%
                           filter(all > 30,ind > mind),cluster = 'opensecretsID'))
  
  tmpPlot <- plot_cme(modInt,variables = 'yellen',condition = c('m'=quantile),draw = F)
  tmpCoef <- summary(modInt)$coeftable %>%
    data.frame() %>%
    mutate(covs = row.names(.)) %>%
    as_tibble() %>%
    filter(grepl(':',covs)) %>%
    rename(est = Estimate,se = Std..Error,tstat = t.value,pval = Pr...t..) %>%
    mutate(stars = ifelse(pval < .001,'***',
                          ifelse(pval < .01,'**',
                                 ifelse(pval < .05,'*',
                                        ifelse(pval < .1,'.',''))))) %>%
    mutate(estLab = paste0(round(est,3),stars,'\n(',round(se,3),')'))
  
  toplot <- bind_rows(toplot,
                      tmpPlot %>%
                        select(estimate,conf.low,conf.high,m) %>%
                        bind_cols(tmpCoef) %>%
                        mutate(m = factor(m)) %>%
                        mutate(mod = ms))
}


toplot <- toplot %>%
  mutate(mod = factor(gsub('Male','Gender',
                           gsub('Gop','Party',
                                str_to_title(gsub('votepct_rel','Vote Margin',
                                                  gsub('nominate_dim1','Ideology',
                                                       gsub('Bill','Fed Oversight',
                                                            gsub('yellen_vote','Yellen Confirmation',
                                                                 gsub('senate','Chamber',
                                                                      gsub('any','',mod))))))))),
                      levels = c('Age','Seniority','Gender',
                                 'Party','Ideology','Chamber',
                                 'Vote Margin','Fed Oversight','Yellen Confirmation',
                                 'Kids','Sons','Daughters'))) 

lookup <- toplot %>%
  select(m,mod)

lookup$xLab <- factor(c('Dem','GOP',
                 'Female','Male',
                 '32','57','64','69','88',
                 '<0.01','0.26','0.61','1',
                 '0','2','7','15','40',
                 '-0.76','-0.16','0.0','0.38','0.86',
                 'Others','Sponsor',
                 'All Others','Oppose/Abstain',
                 'House','Senate',
                 'None','Any',
                 'No Sons','Any Sons',
                 'No Kids','Any Kids'),
                 levels = c('Dem','GOP',
                            'Female','Male',
                            '32','57','64','69','88',
                            '<0.01','0.26','0.61','1',
                            '0','2','7','15','40',
                            '-0.76','-0.16','0.0','0.38','0.86',
                            'Others','Sponsor',
                            'All Others','Oppose/Abstain',
                            'House','Senate',
                            'None','Any',
                            'No Sons','Any Sons',
                            'No Kids','Any Kids'))

toplot  <- toplot %>%
  left_join(lookup) 

pdf('./output/figures/SI_figure_9.pdf',width = 8,height = 8)
toplot %>%
  ggplot(aes(x = xLab,y = estimate,label = estLab)) + 
  geom_hline(yintercept = 0,linetype = 'dashed') + 
  geom_point() + 
  geom_errorbar(aes(ymin = conf.low,ymax = conf.high),width = .2) + 
  geom_label(data = toplot %>%
              group_by(mod,estLab) %>%
              summarise(xLab = diff(range(as.numeric(xLab)))/2+1,
                        estimate = mean(estimate)) %>%
              ungroup(),
            vjust = 1) +
  facet_wrap(~mod,scales = 'free_x',ncol = 3) + xlab('Moderator Value') + ylab('Increased Probability of Interrupting Yellen') + 
  theme_ridges()
dev.off()

# EOF